package java_util.web;


import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class C_WebServer {

    private static int PORT = 8899;
    public static String charset = "utf-8";
    private ServerSocket ss;
    private Socket client;

    public static void S_WebListener(int PORT) {
        new C_WebServer(PORT);
    }

    public C_WebServer(int PORT) {
        C_WebServer.PORT = PORT;
        try {
            this.ss = new ServerSocket(C_WebServer.PORT);
            System.out.println("服务器侦听端口 " + C_WebServer.PORT + "...");
            boolean isGo = true;
            while (isGo) {
                this.client = this.ss.accept();
                System.out.println("已连接一个客户端. " + this.client);
                C_WebClientThread ct = new C_WebClientThread(this.client);
                Thread t = new Thread(ct);
                t.start();
                System.out.println("一个客户端线程已经启动...");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (this.ss != null) {
                    this.ss.close();
                    this.ss = null;
                }
                if (this.client != null) {
                    this.client.close();
                    this.client = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}